<template>
    <div class="login">
        <div class="login-container">
            <el-form :model="loginForm" :rules="rules" ref="loginform" label-position="left" label-width="0px"
                class="login-form">
                <h3 class="title">arcbbs</h3>
                <el-form-item prop="username">
                    <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item prop="checkcode">
                    <el-input style="width: 60%;margin-right: 10px;" v-model="loginForm.checkcode" autocomplete="off"
                        placeholder="请输入验证码"></el-input>
                    <img :src="src" alt="" style="width: 100px;height: 50px;" />
                </el-form-item>


                <el-form-item style="width: 100%;">
                    <el-button type="primary" style="width: 100%;" @click="handleLogin">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script setup>
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";

import { getCurrentInstance } from "vue";
import { useRoute, useRouter } from "vue-router";
import  service  from "../utils/request"
const router=useRouter()
const loginform = ref();
const src = ref("");
const loginForm = ref({});
const rules = {
    checkcode: [
        { required: true, message: '请输入验证码', trigger: 'blur' },
    ],

    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ]
}

const handleLogin = () => {
    loginform.value.validate(async (valid) => {
        console.log(valid)
        if (valid) {
            const res = await service.get("/admin/login", {
                params: loginForm.value
            })
            console.log(res)
            if(res.code ==0){
                getsrc()
                ElMessage({
                    message:res.msg,
                    type:"warning"
                })
            }else{
                router.push({
                    path:"/index"
                })
            }
        }

    })
}
const inputforce = () => {
    let { ctx: that, proxy } = getCurrentInstance()
    that.$forceUpdate()

}
onMounted(() => {
    getsrc()
})
const getsrc = () => {
    src.value = "/api/admin/checkcode?time=" + new Date().getTime()
}
</script>
<style scoped>
.login {
    width: 100%;
    height: 100%;
    background: url("../assets/background.jpg") no-repeat;
    background-size: 100% 100%;
}

.login-container {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.title {
    text-align: center;
}
</style>